package com.baike.controller;

import com.alibaba.fastjson.JSONObject;
import com.baike.data.Result;
import com.baike.em.ConstantEnum;
import com.baike.entity.TbContentOrder;
import com.baike.mapper.TbContentOrderMapper;
import com.baike.util.IdWorker;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping("adOrder")
@Api("广告订单接口")
@RefreshScope
public class AdvertisingOrderController {

    //订单详情mapper
    @Resource
    private TbContentOrderMapper contentOrderMapper;

    //广告订单生成
    @ApiOperation(value = "广告订单生成接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "contentOrder",value = "广告订单实体类",required = true,paramType = "body")
    })
    @PostMapping
    public Result insertAdOrder(@RequestBody TbContentOrder contentOrder){
        //多文件上传json存储
//        List list = new ArrayList();
//        for (MultipartFile multipartFile : file) {
//            Result upload = UploadDownloadUtil.upload(multipartFile);
//            list.add(upload.getResult());
//        }
//        String imgJsonString = JSONObject.toJSONString(list);
        //存储图片
        contentOrder.setCimage(contentOrder.getCimage());
        //生成订单id
        contentOrder.setOrderId(new IdWorker().nextId());
        //生成订单号
        contentOrder.setOutTradeNo(new IdWorker().nextId()+"");
        //订单状态
        contentOrder.setStatus(1);
        contentOrderMapper.insert(contentOrder);
        return Result.build(ConstantEnum.SUCCESS).setResult("OK!");
    }

    //广告订单查询所有接口
    @ApiOperation(value = "广告订单查询所有接口")
    @GetMapping
    public Result findAdOrderAll(){
        List<TbContentOrder> tbContentOrders = contentOrderMapper.selectList(null);
        return Result.build(ConstantEnum.SUCCESS).setResult(tbContentOrders);
    }

    //广告订单id查询接口
    @ApiOperation(value = "广告订单id查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "广告订单id",required = true,paramType = "path")
    })
    @GetMapping("findAdOrderById/{id}")
    public Result findAdOrderById(@PathVariable("id")Long id){
        TbContentOrder contentOrder = contentOrderMapper.selectById(id);
        return Result.build(ConstantEnum.SUCCESS).setResult(contentOrder);
    }

    //广告订单修改接口
    @ApiOperation(value ="广告订单修改接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "contentOrder",value = "广告订单实体类,id必填",required = true,paramType = "body")
    })
    @PutMapping
    public Result updateAdOrderById(@RequestBody TbContentOrder contentOrder){
        try {
            //TODO cs:该修改方法会覆盖字段update_time,待解决
            int i = contentOrderMapper.updateById(contentOrder);
            if (i>0){
                return Result.build(ConstantEnum.SUCCESS).setResult("修改成功~");
            }
            return Result.build(ConstantEnum.SUCCESS).setResult("修改失败~");
        }catch (Exception e){
            e.printStackTrace();
            return Result.build(ConstantEnum.ERROR).setResult("修改失败~");
        }
    }

    //广告订单删除接口
    @ApiOperation(value = "订单删除接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",value = "广告订单id/多选",required = true,paramType = "query")
    })
    @DeleteMapping
    public Result deleteAdOrderById(@RequestParam("ids")Long[] ids){
        try {
            contentOrderMapper.deleteBatchIds(Arrays.asList(ids));
            return Result.build(ConstantEnum.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return Result.build(ConstantEnum.ERROR);
        }
    }

    //根据订单号修改订单状态
    @ApiOperation(value ="根据订单号修改接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "outTradeNo",value = "订单号",required = true,paramType = "query"),
            @ApiImplicitParam(name = "tradeNo",value = "支付宝交易号",required = true,paramType = "query")
    })
    @PutMapping("updateAdOrderByOutTradeNo")
    public Result updateAdOrderByOutTradeNo(@RequestParam("outTradeNo") String outTradeNo,@RequestParam("tradeNo") String tradeNo){
        try {
            TbContentOrder tbContentOrder = new TbContentOrder();
            tbContentOrder.setTradeNo(tradeNo);
            tbContentOrder.setStatus(2);
            tbContentOrder.setEndTime(new Date());
            UpdateWrapper<TbContentOrder> wrapper = new UpdateWrapper<>();
            wrapper.eq("out_trade_no",outTradeNo);
            contentOrderMapper.update(tbContentOrder,wrapper);
            return Result.build(ConstantEnum.SUCCESS).setResult("修改成功~");
        }catch (Exception e){
            e.printStackTrace();
            return Result.build(ConstantEnum.ERROR).setResult("修改失败~");
        }
    }
    //超时未支付修改状态
    @ApiOperation(value ="超时未支付修改状态接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "outTradeNo",value = "订单号",required = true,paramType = "query"),
    })
    @PutMapping("TimeOutAdOrderByOutTradeNo")
    public Result TimeOutAdOrderByOutTradeNo(@RequestParam("outTradeNo") String outTradeNo){
        try {
            //先判断订单是否超时
            Result result = findAdOrderByOutTradeNo(outTradeNo);
            TbContentOrder order = JSONObject.parseObject(result.getResult().toString(), TbContentOrder.class);
            if ("1".equals(order.getStatus().toString())){
                //超时未支付,修改状态
                TbContentOrder tbContentOrder = new TbContentOrder();
                tbContentOrder.setStatus(4);//超时未支付
                UpdateWrapper<TbContentOrder> wrapper = new UpdateWrapper<>();
                wrapper.eq("out_trade_no",outTradeNo);
                contentOrderMapper.update(tbContentOrder,wrapper);
                return Result.build(ConstantEnum.SUCCESS).setResult("修改成功~");
            }
            //其他状态不做处理
            return Result.build(ConstantEnum.SUCCESS).setResult("交易已完成~");
        }catch (Exception e){
            e.printStackTrace();
            return Result.build(ConstantEnum.ERROR).setResult("修改失败~");
        }
    }

    //广告订单号查询接口
    @ApiOperation(value = "广告订单id查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "OutTradeNo",value = "广告订单号",required = true,paramType = "path")
    })
    @GetMapping("/{OutTradeNo}")
    public Result findAdOrderByOutTradeNo(@PathVariable("OutTradeNo")String OutTradeNo){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("out_trade_no",OutTradeNo);
        TbContentOrder contentOrder = contentOrderMapper.selectOne(wrapper);
        String jsonString = JSONObject.toJSONString(contentOrder);
        return Result.build(ConstantEnum.SUCCESS).setResult(jsonString);
    }

}
